package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.status.ImMessageStatus;
import ldh.im.fxbase.richtext.*;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.richtext.item.EmojiTextItem;
import ldh.im.fxbase.richtext.item.ImageTextItem;
import ldh.im.fxbase.util.ImRichTextUtil;
import org.fxmisc.flowless.Cell;
import org.fxmisc.flowless.VirtualFlow;
import org.fxmisc.richtext.model.ReadOnlyStyledDocument;
import org.reactfx.util.Either;

public class ImTextPaneTest extends Application {

    private ObservableList<ImMessage> items = FXCollections.observableArrayList();
    private VirtualFlow<ImMessage, ?> messageList;
    private ImTextArea imTextArea = new ImTextArea();

    @Override
    public void start(Stage primaryStage) throws Exception {
        messageList = VirtualFlow.createVertical(items, m->{
            return Cell.wrapNode(createMessagePane(m));
        });

        VBox vBox = new VBox();
        Button b = new Button("send");
        vBox.getChildren().addAll(messageList, createToolBar(),  imTextArea, b);
        VBox.setVgrow(messageList, Priority.ALWAYS);
        b.setOnAction(e->{
            String msg = imTextArea.getData();
            if (msg.equals("")) return;
            int richTextType = ImRichTextUtil.richTextType(msg);
            MessageType messageType = richTextType == 1 ? MessageType.Text : (richTextType == 2 ? MessageType.Image : MessageType.RichText);
            ImMessage textMessage = new ImMessage();
            textMessage.setContent(msg);
            textMessage.setMessageType(messageType);
            textMessage.setStatus(ImMessageStatus.created);
            items.add(textMessage);
            imTextArea.clear();
            messageList.show(items.size());
        });

        Scene scene = new Scene(vBox, 800, 600);
        scene.getStylesheets().add(this.getClass().getResource("/demo/texta.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        imTextArea.requestFocus();
    }

    private Node createMessagePane(ImMessage m) {
        ImTextPane imTextPane = new ImTextPane(null);
        imTextPane.setImContent(m.getContent());
        imTextPane.setMaxWidth(300);
        imTextPane.setPrefWidth(300);
        imTextPane.setMinWidth(300);
        imTextPane.getTextFlowExt().setMaxWidth(300);
        imTextPane.getTextFlowExt().setPrefWidth(300);
        imTextPane.getTextFlowExt().setMinWidth(300);
        return new Group(imTextPane);
    }

    private HBox createToolBar() {
        HBox toolBar = new HBox();
        Button insertEmoji = new Button("insert emoji");
        insertEmoji.setOnAction(e->{
            ReadOnlyStyledDocument<Void, Either<String, TextItem>, String> ros =
                    ReadOnlyStyledDocument.fromSegment(Either.right(new EmojiTextItem("1f1e7-1f1e6")), null, "emoji", imTextArea.getSegOps());
            imTextArea.replaceSelection(ros);
        });

        Button insertImage = new Button("insert image");
        insertImage.setOnAction(e->{
            ReadOnlyStyledDocument<Void, Either<String, TextItem>, String> ros =
                    ReadOnlyStyledDocument.fromSegment(Either.right(new ImageTextItem("E:\\test\\11.jpg")), null, "image", imTextArea.getSegOps());
            imTextArea.replaceSelection(ros);
        });

        Button copy = new Button("copy");
        copy.setOnAction(e->{
            imTextArea.copy();
        });

        Button paste = new Button("paste");
        paste.setOnAction(e->{
            imTextArea.paste();
        });


        Button getData = new Button("Get Data");
        getData.setOnAction(e->{
            System.out.println("data:" + imTextArea.getData());
        });

        toolBar.getChildren().addAll(insertEmoji, insertImage, copy, paste, getData);
        return toolBar;
    }
}
